/*
 * @Author: your name
 * @Date: 2021-06-23 11:33:25
 * @LastEditTime: 2021-07-01 10:08:06
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \01e:\电商管理\app\src\router\index.js
 */
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: () => import('../components/Login.vue')
  },
  {
    path: '/home',
    component: () => import('../components/Home.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {

  // to将要访问的路径
  // 2.from代表从那个路径跳转而来
  // 3.next 是一个函数 ，表示放行
  // 4.next()放行  next('/login')强制跳转

  if (to.path === '/login') return next();
  // 先获取token，
  const tokens = window.sessionStorage.getItem('token')
  if (!tokens) return next('/login')
  next()
})
export default router
